package leetcode.code0539;

import java.util.Collections;
import java.util.List;

import leetcode.helper.HelpDebug;

/**
 * 执行用时：10 ms, 在所有 Java 提交中击败了27.25%的用户
 *
 * 内存消耗：37.8 MB, 在所有 Java 提交中击败了94.61%的用户
 *
 * 通过测试用例：113 / 113
 *
 * 执行用时：9 ms, 在所有 Java 提交中击败了30.01%的用户
 *
 * 内存消耗：37.6 MB, 在所有 Java 提交中击败了98.76%的用户
 *
 * 通过测试用例：113 / 113
 */
public class Solution {

	public int findMinDifference(List<String> timePoints) {
		int size = timePoints.size();
//		String[] times = timePoints.toArray(new String[size]);
//		Arrays.sort(times);
		Collections.sort(timePoints);
		int pre = this.str2int(timePoints.get(0));
		int ans = 24 * 60 + pre - this.str2int(timePoints.get(size - 1));
		for (int i = 1; i < size; i++) {
			int cur = this.str2int(timePoints.get(i));
			ans = Math.min(ans, cur - pre);
			pre = cur;
		}
		return ans;
	}

	private int str2int(String time) {
		return ((time.charAt(0) - '0') * 10 + (time.charAt(1) - '0')) * 60 + (time.charAt(3) - '0') * 10
				+ (time.charAt(4) - '0');
	}

	public static void main(String[] args) {
		Solution so = new Solution();
		HelpDebug.compare(1, so.findMinDifference(HelpDebug.str2liststr("[\"23:59\",\"00:00\"]")));
		HelpDebug.compare(0, so.findMinDifference(HelpDebug.str2liststr("[\"00:00\",\"23:59\",\"00:00\"]")));
	}
}
